package com.dream.dao.base;

/**
 * 
 * @author Evan
 * @date 2013-8-25
 * 
 */
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Repository;

import com.dream.model.app.AppUserGroup;
import com.dream.utils.hibernate.DetachedCriteriaBuilder;
import com.dream.utils.hibernate.DreamBaseDAO;

@Repository
public class AppUserGroupDAO extends DreamBaseDAO<AppUserGroup> {
	
	/**
	 * 
	 * @copyright evan
	 * @author evan
	 * @date 2013-9-3
	 * @param userId
	 * @param groupId
	 * @return
	 */
	public boolean existed(final String userId,final String groupId){
		
		DetachedCriteriaBuilder builder = DetachedCriteriaBuilder.instance(AppUserGroup.class);
		builder.addEq("userId", userId).addEq("groupId", groupId).addEq("deleted", false);
		return CollectionUtils.isNotEmpty(select(builder));
		
	}
	
	/**
	 * 
	 * @copyright evan
	 * @author evan
	 * @date 2013-9-3
	 * @param userId
	 * @return
	 */
	public List<AppUserGroup> getByUser(final String userId){
		
		DetachedCriteriaBuilder builder = DetachedCriteriaBuilder.instance(AppUserGroup.class);
		builder.addEq("userId", userId).addEq("deleted", false);
		builder.addAscOrder("id");
		return select(builder);
	}
	
	/**
	 * 
	 * @copyright evan
	 * @author evan
	 * @date 2013-9-3
	 * @param userId
	 * @return
	 */
	public Integer deleteByUser(final String userId){
		
		final String DEL_USER_GROUP = "delete  AppUserGroup where userId = ?";
		return bulkUpdate(DEL_USER_GROUP, userId);
		
	}
	
}
